<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>9.享元模式</title>
</head>
<body>
	享元模式的核心是运用共享技术来有效支持大量细粒度的对象
</body>
<script type="text/javascript">
	var Upload = function(uploadType){
		this.uploadType = uploadType;
	}
	Upload.prototype.delFile = function(id) {
		uploadManager.setExternalState(id,this);
		if(this.fileSize < 3000){
			return this.dom.parentNode.removeChild( this.dom );
		}
		if(window.confirm( '确定删除' + this.fileName )){
			return this.dom.parentNode.removeChild( this.dom );
		}
	};

	var UploadFactory = (function(){
		var createdFlyWeightObjs = {};

		return {
			create : function(uploadType){
				if(createdFlyWeightObjs[ uploadType ]){
					return createdFlyWeightObjs[ uploadType ]
				}
				return createdFlyWeightObjs[ uploadType ] = new Upload( uploadType );
			}
		}
	})();

	var uploadManager = (function(){
		var uploadDatabase = {};

		return {
			add : function(id,uploadType,fileName,fileSize){
				var flyWeightObj = UploadFactory.create( uploadType );
				var dom = document.createElement('div');
				dom.innerHTML = '<span>文件名：'+ fileName +',文件大小：'+ fileSize +'</span><button class="delFile">删除</button>';
				dom.querySelector('.delFile').onclick = function(){
					flyWeightObj.delFile(id);
				}
				document.body.appendChild(dom);
				uploadDatabase[id] = {
					fileName : fileName,
					fileSize : fileSize,
					dom : dom
				};
				return flyWeightObj;
			},
			setExternalState : function(id,flyWeightObj){
				var uploadData = uploadDatabase[ id ];
				for(var i in uploadData){
					flyWeightObj[i] = uploadData[i];
				}
			}
		}
	})()


	var id = 0;
	window.startUpload = function(uploadType,files){
		for(var i = 0,file;file = files[i++];){
			var uploadObj = uploadManager.add( ++id, uploadType, file.fileName, file.fileSize );
		}
	}

	startUpload('plugin',[
		{
			fileName:'1.txt',
			fileSize:1000
		},
		{
			fileName:'2.txt',
			fileSize:2000
		},
		{
			fileName:'3.txt',
			fileSize:3000
		}
	])
</script>
</html>